require 'radiator'
class Hive
  def initialize()
    # @options = {
    #     # url:'https://api.openhive.network'
    # }
    @options = {
        url:CHAIN_URL,
    }
  end

  def sendSignedOperation(json = '')
    tx = Radiator::Transaction.new @options
    tx.broadcast_payload(JSON.parse(json))
  end

  def get_dynamic_global_properties
    api = Radiator::Api.new @options
    api.get_dynamic_global_properties
  end

  def getContent(status = Status.new)
    api = Radiator::Api.new(@options)
    api.get_content(status.account.username,status.perm_link)
  end

  def updateContent(status = Status.new)
    api = Radiator::Api.new(@options)
    HiveLog.log status
    api.get_content(status.account.username,status.perm_link) do |content,error|
      HiveLog.log content
      HiveLog.log error
      pending_payout_value = content.pending_payout_value.sub!(/\s[A-Z]*$/,'')
      curator_payout_value = content.curator_payout_value.sub!(/\s[A-Z]*$/,'')
      total_payout_value = content.total_payout_value.sub!(/\s[A-Z]*$/,'')
      status = Status.find_by_perm_link(content.permlink)
      status.pending_payout_value = pending_payout_value
      status.curator_payout_value = curator_payout_value
      status.total_payout_value = total_payout_value
      status.save
    end
  end


  def createAccount(param){

  }
  end

  private
  # 发文和评论
  def comment(status = Status.new)
    permlink = status.id.to_s
    in_reply_to_account_id = status.in_reply_to_account_id
    parent_author = in_reply_to_account_id ? status.in_reply_to_account.username : ''
    tags = %w(mastodon)
    in_reply_to_id = status.in_reply_to_id
    parent_permlink = in_reply_to_id ? in_reply_to_id.to_s : tags[0]
    title = Time.new.to_s
    medias_url = ""
    media_attachments = status.media_attachments
    media_attachments.each do |media|
      medias_url += "![#{media.file_file_name}](https://images.hive.blog/DQmUZoPKmsfhGT1LaZ1XBgLomntMjSbyX3vwaBMoHviyhdB/lake.jpg)"
    end

    body = status.text + medias_url

    metadata = {
        tags: tags,
        status:status,
        media_attachments:media_attachments
    }
    json_metadata = metadata.to_json

    tx = Radiator::Transaction.new @options

    tx.operations << {
        type: :comment,
        author: @username,
        permlink: permlink,
        parent_author: parent_author,
        parent_permlink: parent_permlink,
        title: title,
        body: body,
        json_metadata: json_metadata
    }
    tx.process(true)
  end

  # 投票喜欢，取消喜欢
  def vote(status = Status.new,like = 100 )
    author = status.account.username
    permlink = status.id.to_s
    weight = like * 100
    tx = Radiator::Transaction.new(@options)

    tx.operations << {
        type: :vote,
        voter: @username,
        author: author,
        permlink: permlink,
        weight: weight
    }
    tx.process(true)
  end
  # 关注，取消关注
  def follow(following,unfollow = false )
    tx = Radiator::Transaction.new(@options)
    # what = 'blog' # set this to empty string to unfollow
    what = unfollow ? '' : 'blog'
    tx.operations << {
        type: :custom_json,
        id: 'follow',
        required_auths: [],
        required_posting_auths: [@username],
        json: [:follow, {
            follower: @username,
            following: 'blocktrades',# 脏数据
            what: [what]
        }].to_json
    }

    tx.process(true)
  end

  def reblog(status = Status.new)
    tx = Radiator::Transaction.new(@options)
    author = status.account.username
    permlink = status.id.to_s
    data = [
        :reblog, {
        account: @username,
        author: "lukewearechange",
        permlink: "trump-called-fauci-an-idiot-but-the-bigger-story-is-being-missed-here"
    }
    ]

    tx.operations << {
        type: :custom_json,
        id: 'follow',
        required_auths: [],
        required_posting_auths: [@username],
        json: data.to_json
    }
    tx.process(true)
  end



end
